/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mapfinal.server.auth.web;

import java.io.IOException;
import java.util.Map;

import com.lambkit.common.util.StringUtils;
import com.lambkit.common.service.ServiceKit;
import com.lambkit.web.tag.LambkitTemplateModel;
import com.mapfinal.server.auth.UserService;
import com.mapfinal.server.auth.model.User;
import com.jfinal.kit.StrKit;
import com.jfinal.render.FreeMarkerRender;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2020-09-23
 * @version 1.0
 * @since 1.0
 */
/**
 * lk_users标签<br>
 * 参数：{id:主键}
 * 返回值：{entity:lk_users信息}
 * @author lambkit
 */
public class UsersMarker extends LambkitTemplateModel {

	@Override
	public void onRender(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		// TODO Auto-generated method stub
		String id = get(params, "id");
		String uuid = get(params, "uuid");
		String userLogin = get(params, "user_login");
		String userPass = get(params, "user_pass");
		String userNicename = get(params, "user_nicename");
		String userEmail = get(params, "user_email");
		String userUrl = get(params, "user_url");
		String avatar = get(params, "avatar");
		String sex = get(params, "sex");
		String birthday = get(params, "birthday");
		String signature = get(params, "signature");
		String lastLoginIp = get(params, "last_login_ip");
		String lastLoginTime = get(params, "last_login_time");
		String createTime = get(params, "create_time");
		String userActivationKey = get(params, "user_activation_key");
		String userStatus = get(params, "user_status");
		String score = get(params, "score");
		String userType = get(params, "user_type");
		String salt = get(params, "salt");
		String userPhone = get(params, "user_phone");
		int pagenum = getInt(params, "pagenum", 0);
		int pagesize = getInt(params, "pagesize", 0);
		String wheresql = get(params, "sql", null);
		String sql = " from lk_users where "; 
		if(wheresql == null) {
			sql += " 1=1 ";
			if(StringUtils.hasText(id)) sql += " and id=" + id;//bigserial
			if(StringUtils.hasText(uuid)) sql += " and uuid like '%" + uuid + "%'";//varchar
			if(StringUtils.hasText(userLogin)) sql += " and user_login like '%" + userLogin + "%'";//varchar
			if(StringUtils.hasText(userPass)) sql += " and user_pass like '%" + userPass + "%'";//varchar
			if(StringUtils.hasText(userNicename)) sql += " and user_nicename like '%" + userNicename + "%'";//varchar
			if(StringUtils.hasText(userEmail)) sql += " and user_email like '%" + userEmail + "%'";//varchar
			if(StringUtils.hasText(userUrl)) sql += " and user_url like '%" + userUrl + "%'";//varchar
			if(StringUtils.hasText(avatar)) sql += " and avatar like '%" + avatar + "%'";//varchar
			if(StringUtils.hasText(sex)) sql += " and sex=" + sex;//int2
			if(StringUtils.hasText(birthday)) sql += " and birthday like '%" + birthday + "%'";//date
			if(StringUtils.hasText(signature)) sql += " and signature like '%" + signature + "%'";//varchar
			if(StringUtils.hasText(lastLoginIp)) sql += " and last_login_ip like '%" + lastLoginIp + "%'";//varchar
			if(StringUtils.hasText(lastLoginTime)) sql += " and last_login_time like '%" + lastLoginTime + "%'";//timestamp
			if(StringUtils.hasText(createTime)) sql += " and create_time like '%" + createTime + "%'";//timestamp
			if(StringUtils.hasText(userActivationKey)) sql += " and user_activation_key like '%" + userActivationKey + "%'";//varchar
			if(StringUtils.hasText(userStatus)) sql += " and user_status=" + userStatus;//int4
			if(StringUtils.hasText(score)) sql += " and score=" + score;//int4
			if(StringUtils.hasText(userType)) sql += " and user_type=" + userType;//int2
			if(StringUtils.hasText(salt)) sql += " and salt like '%" + salt + "%'";//varchar
			if(StringUtils.hasText(userPhone)) sql += " and user_phone like '%" + userPhone + "%'";//varchar
		} else {
			sql += wheresql;
		}
		
		String orderby = get(params, "orderby", null);
		
		if(StrKit.notBlank(orderby)) {
			sql += " order by " + orderby;
		}
		
		UserService service = User.service();
		
		String tagEntityKeyname = get(params, "key", "entity");
		if(pagenum==0) {
			env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(
					service.dao().findFirst("select *" + sql)));
		} else {
			if(pagesize==0) {
				env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(
						service.dao().find("select *" + sql)));
			} else {
				env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(
						service.dao().paginate(pagenum, pagesize, "select *", sql)));
			}
		}
        body.render(env.getOut());
	}
}
